package io.renren.modules.com.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.xiaoleilu.hutool.util.StrUtil;

import io.renren.modules.com.dao.ComImageDao;
import io.renren.modules.com.entity.ComImageEntity;
import io.renren.modules.com.service.ComImageService;
import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
@Service("comImageService")
public class ComImageServiceImpl implements ComImageService {

	private Logger logger = LoggerFactory.getLogger(ComImageServiceImpl.class);

	@Autowired
	private ComImageDao comImageDao;

	@Value("${spring.upload.path:#{null}}")
	private String uploadPath;

	@Override
	public ComImageEntity queryObject(Long imageId) {
		return comImageDao.queryObject(imageId);
	}

	@Override
	public List<ComImageEntity> queryList(Map<String, Object> map) {
		return comImageDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return comImageDao.queryTotal(map);
	}

	@Override
	public void save(ComImageEntity comImage) {
		comImageDao.save(comImage);
	}

	@Override
	public void update(ComImageEntity comImage) {
		comImageDao.update(comImage);
	}

	@Override
	public void delete(Long imageId) {
		comImageDao.delete(imageId);
	}

	@Override
	public void deleteBatch(Long[] imageIds) {
		comImageDao.deleteBatch(imageIds);
	}

	@Override
	public Long upload(MultipartFile multipartFile) {
		return this.uploadPrivate(multipartFile);

	}

	private Long uploadPrivate(MultipartFile multipartFile) {
		String fileNo = String.valueOf(System.currentTimeMillis());
		File dir = new File(uploadPath);
		boolean flag = dir.mkdirs();
		if (flag) {
			logger.info("创建了目录{}", dir.getPath());
		}
		String originalName = multipartFile.getOriginalFilename();
		String diskFileName = null;
		if (StrUtil.isNotBlank(originalName)) {
			diskFileName = fileNo + originalName.substring(originalName.lastIndexOf("."));
			File source = new File(dir.getPath() + File.separator + diskFileName);
			try {
				multipartFile.transferTo(source);
				ComImageEntity image = new ComImageEntity();
				image.setFileNo(fileNo);
				image.setUploadPath(source.getPath());
				comImageDao.save(image);
				return image.getImageId();
			} catch (IllegalStateException e) {
				logger.error("文件上传失败");
			} catch (IOException e) {
				logger.error("文件上传失败，IO异常");
			}
		}
		return null;
	}

	@Override
	public String getPhoto(Long imageId) {
		return this.getPhotoPrivate(imageId);
	}

	private String getPhotoPrivate(Long imageId) {
		ComImageEntity image = comImageDao.queryObject(imageId);
		if (image == null) {
			return null;
		}

		InputStream inputStream = null;
		byte[] data = null;
		try {
			String path = image.getUploadPath();
			inputStream = new FileInputStream(path);
			data = new byte[inputStream.available()];
			inputStream.read(data);
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 加密
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);
	}

}
